package com.nowcoder.community.service.impl;

import com.nowcoder.community.service.LikeService;
import com.nowcoder.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /*
    @Override
    public void like(int userId, int entityType, int entityId) {
        String redisKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        // 判断当前用户是否已经点赞
        Boolean liked = redisTemplate.opsForSet().isMember(redisKey, userId);
        // 如果没点过赞,则点赞
        if(Boolean.FALSE.equals(liked)) {
            redisTemplate.execute(new SessionCallback() {
                @Override
                public Object execute(RedisOperations operations) throws DataAccessException {
                    operations.multi();
                    redisTemplate.opsForSet().add(redisKey, userId);
                    return operations.exec();
                }
            });
        }
        // 如果点过赞,则取消
        else {
            redisTemplate.execute(new SessionCallback() {
                @Override
                public Object execute(RedisOperations operations) throws DataAccessException {
                    operations.multi();
                    redisTemplate.opsForSet().remove(redisKey, userId);
                    return operations.exec();
                }
            });
        }
    }

     */

    /**
     *
     * @param userId
     * @param entityType
     * @param entityId
     * @param entityUserId: 被点赞的评论或帖子所对应的用户实体
     */
    @Override
    public void like(int userId, int entityType, int entityId, int entityUserId) {
        redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
                String userLikeKey = RedisKeyUtil.getUserLikeKey(entityUserId);
                // redis查询操作要放在事务管理范围之外
                // 否则数据修改操作结束前不会执行
                boolean isMember = redisTemplate.opsForSet().isMember(entityLikeKey, userId);
                // 开启redis事务
                operations.multi();
                if(isMember) {
                    redisTemplate.opsForSet().remove(entityLikeKey, userId);
                    // 将被点赞目标用户的赞数减1
                    redisTemplate.opsForValue().decrement(userLikeKey);
                }
                else {
                    redisTemplate.opsForSet().add(entityLikeKey, userId);
                    // 将被点赞目标用户的赞数加1
                    redisTemplate.opsForValue().increment(userLikeKey);
                }
                // 关闭redis事务
                // 并返回
                return operations.exec();
            }
        });
    }

    @Override
    public long findEntityLikeCount(int entityType, int entityId) {
        String redisKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        Boolean keyExists = redisTemplate.hasKey(redisKey);
        if(keyExists != null && keyExists) {
            return redisTemplate.opsForSet().size(redisKey);
        }
        else {
            return 0L;
        }
    }

    @Override
    public int findEntityLikeStatus(int userId, int entityType, int entityId) {
        String redisKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
        Boolean status = redisTemplate.opsForSet().isMember(redisKey, userId);
        if(Boolean.TRUE.equals(status)) {
            return 1;
        }
        return 0;
    }

    @Override
    public int findUserLikeCount(int userId) {
        String userLikeKey = RedisKeyUtil.getUserLikeKey(userId);
        Integer count = (Integer) redisTemplate.opsForValue().get(userLikeKey);
        return count == null ? 0 : count;
    }

}
